package com.example.performance.controller;

import com.example.performance.auth.CheckLogin;
import com.example.performance.dto.MemberDto;
import com.example.performance.pojo.OcrCount;
import com.example.performance.service.PerformanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 杨胖胖
 */
@Api(tags = "ocr统计")
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("ocr")
public class OcrController {
    private final PerformanceService service;

    @PostMapping("login")
    public Map<String, String> token(@RequestBody MemberDto memberDto) throws InterruptedException {
        String admin = "admin";
        HashMap<String, String> hashMap = new HashMap<>(2);
        TimeUnit.SECONDS.sleep(3);
        if (admin.equals(memberDto.getUsername()) && admin.equals(memberDto.getPassword())) {
            hashMap.put("token", "9cc23380bc67483d543640610a08b8811e05c22cc");
            return hashMap;
        }
        hashMap.put("message", "用户名或密码错误");
        return hashMap;
    }

    @GetMapping("url-param")
    @ApiOperation("url拼接入参")
    @CheckLogin
    public Map<String, String> urlParamAdd(@RequestParam String version,
                                           @RequestParam String model,
                                           @RequestParam int status) {
        return service.urlParamAdd(new OcrCount(version, model, status));
    }

    @PostMapping("body-param")
    @ApiOperation("body方式传递参数")
    @CheckLogin
    public Map<String, String> bodyParamAdd(@RequestBody OcrCount ocrCount) {
        return service.bodyParamAdd(ocrCount);
    }

    @PutMapping("path-param/{version}/{model}/{status}")
    @ApiOperation("路径传递参数")
    @CheckLogin
    public Map<String, String> urlPathParamAdd(@PathVariable String version,
                                               @PathVariable String model,
                                               @PathVariable int status) {
        return service.urlPathParamAdd(new OcrCount(version, model, status));
    }

    @PostMapping("form-param")
    @ApiOperation("from表单方式传递参数")
    @CheckLogin
    public Map<String, String> formParamAdd(String version,
                                            String model,
                                            int status) {
        return service.formParamAdd(new OcrCount(version, model, status));
    }

    @GetMapping("query-model/{model}")
    @ApiOperation("根据model查询内容")
    @CheckLogin
    public List<OcrCount> queryByModel(@PathVariable String model) {
        return service.queryByModel(model);
    }

    @GetMapping("query-term")
    @ApiOperation("输入model或status条件查询内容，可同时输入")
    @CheckLogin
    public List<OcrCount> queryByModelAndStatus(String model, @RequestParam(required = false) String status) {
        return service.queryByModelAndStatus(model, status);
    }

    @GetMapping("query/{id}")
    @ApiOperation("根据id查询内容，会使用redis缓存")
    @CheckLogin
    public OcrCount queryById(@PathVariable String id) {
        return service.queryById(id);
    }

}
